package com.nuc.iblog;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;

/**
 * Created by Tyranitarx on 2018/1/13.
 *
 * @Description :
 */
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
    private static final Log log = LogFactory.getLog(MyWebAppConfigurer.class);
    private static final String win_path = "D:\\blogFile\\";
    private static final String linux_path = "/Users/blogFile/";
    private static final String winpic_path = "D:\\blogFile\\picture\\";
    private static final String winvideo_path = "D:\\blogFile\\video\\";
    private static final String winmusic_path = "D:\\blogFile\\music\\";
    private static final String linuxpic_path = "/Users/blogFile/picture/";
    private static final String linuxvideo_path = "/Users/blogFile/video/";
    private static final String linuxmusic_path = "/Users/blogFile/music/";


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String os=System.getProperty("os.name");
        log.info(os);
        if(os.toLowerCase().startsWith("win")){
            log.info("当前操作系统为windows");
            createDir(win_path);
            createDir(winmusic_path);
            createDir(winpic_path);
            createDir(winvideo_path);
            registry.addResourceHandler("/blogFile/**").addResourceLocations("file:"+win_path);
        }
        else {
            log.info("当前操作系统为linux");
            createDir(linux_path);
            createDir(linuxpic_path);
            createDir(linuxmusic_path);
            createDir(linuxvideo_path);
            registry.addResourceHandler("/blogFile/**").addResourceLocations("file:"+linux_path);
        }
        super.addResourceHandlers(registry);
    }

    private void createDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            log.info("文件目录不存在");
            log.info("创建：" + path);
            boolean mkdir = file.mkdirs();
            if (!mkdir) {
                log.warn("创建：" + path + "失败");
            }
        } else {
            log.info("文件目录存在");
        }
    }

    public static String picsPath() {
        String os = System.getProperty("os.name");
        log.info(os);
        if (os.toLowerCase().startsWith("win")) {
            return winpic_path;
        } else {
            return linuxpic_path;
        }
    }
    public static String musicPath() {
        String os = System.getProperty("os.name");
        log.info(os);
        if (os.toLowerCase().startsWith("win")) {
            return winmusic_path;
        } else {
            return linux_path;
        }
    }
    public static String videoPath() {
        String os = System.getProperty("os.name");
        log.info(os);
        if (os.toLowerCase().startsWith("win")) {
            return winvideo_path;
        } else {
            return linuxvideo_path;
        }
    }
    public static String Pathpic() {
        String os = System.getProperty("os.name");
        log.info(os);
        if (os.toLowerCase().startsWith("win")) {
            return win_path;
        } else {
            return linux_path;
        }
    }

}
